import Promise from 'bluebird';
import mongoose from 'mongoose';
import logger from '../common/logger';
import { mongodbUrl } from '../../config';

mongoose.Promise = Promise;
mongoose.connect(mongodbUrl, { server: { poolSize: 20 } });

const db = mongoose.connection;

db.on('error', (err) => {
  logger.error('connect to %s error: ', mongodbUrl, err.message);
  process.exit(1);
});

db.once('open', () => {
  logger.info('connect to %s success', mongodbUrl);
});

// models
require('./user');
require('./topic');
require('./reply');
require('./topic_collect');
require('./message');
require('./zone');
require('./menu');
require('./submenu');
require('./profile');
require('./audio');
require('./static');
require('./repo');

export var UserModel = mongoose.model('User');
export var TopicModel = mongoose.model('Topic');
export var ReplyModel = mongoose.model('Reply');
export var TopicCollectModel = mongoose.model('TopicCollect');
export var MessageModel = mongoose.model('Message');
export var ZoneModel = mongoose.model('Zone');
export var MenuModel = mongoose.model('Menu');
export var SubmenuModel = mongoose.model('Submenu');
export var ProfileModel = mongoose.model('Profile');
export var AudioModel = mongoose.model('Audio');
export var StaticModel = mongoose.model('Static');
export var RepoModel = mongoose.model('Repo');
